#include "function_s.h"
#include <func.h>

void set_nonblock(int fd){
    int flag=fcntl(fd,F_GETFL,0);
    flag|=O_NONBLOCK;
    fcntl(fd,F_SETFL,flag);
}

int create_connect(int port,const char* ip){
    int listenfd=socket(AF_INET,SOCK_STREAM,0);
    int on=1;
    setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(port);
    addr.sin_addr.s_addr=inet_addr(ip);
    bind(listenfd,(struct sockaddr*)&addr,sizeof(addr));
    listen(listenfd,100);
    return listenfd;
}

int create_epoll(int fd){
    int efd=epoll_create1(0);
    add_r_epoll(efd,fd);
    return efd;
}

void del_epoll(int efd,int fd){
    struct epoll_event listevent;
    listevent.data.fd=fd;
    epoll_ctl(efd,EPOLL_CTL_DEL,fd,&listevent);
}

void add_rw_epoll(int efd,int fd){
    struct epoll_event listevent;
    memset(&listevent,0,sizeof(listevent));
    listevent.data.fd=fd;
    listevent.events=EPOLLIN|EPOLLOUT;
    epoll_ctl(efd,EPOLL_CTL_ADD,fd,&listevent);
}

void add_r_epoll(int efd,int fd){
    struct epoll_event listevent;
    memset(&listevent,0,sizeof(listevent));
    listevent.data.fd=fd;
    listevent.events=EPOLLIN;
    epoll_ctl(efd,EPOLL_CTL_ADD,fd,&listevent);
}

